package tiepbm.app.store.fragment;

import java.util.ArrayList;

import tiepbm.app.store.activity.DetailActivity;
import tiepbm.app.store.activity.R;
import tiepbm.app.store.adapter.CategoryAdapter;
import tiepbm.app.store.object.Obj_App;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

public class FragmentCategory extends Fragment {
	private ListView lsvApp;
	private CategoryAdapter adapter;
	private ArrayList<Obj_App> listApp = new ArrayList<Obj_App>();
	private boolean isLoading = false, isLoadMore = true;
	private String[] name = { "tiepbm", "tiepbm", "tiepbm", "tiepbm", "tiepbm",
			"tiepbm", "tiepbm" };
	private String[] develop = { "inet", "inet", "inet", "inet", "inet",
			"inet", "inet" };
	private int[] rate = { 1, 2, 3, 0, 4, 5, 0 };
	private int[] type = { 0, 0, 0, 0, 0, 0, 0 };
	private String[] icon = {
			"http://iphim.vn/Shared/Posters/ae731b1ef31b441192120b38aca4beffNhung-thien-than-denh.jpg",
			"http://iphim.vn/Shared/Posters/97617b0393d8443cbe0d16ec5327c57aAsterix-o-the-van-hoi-Olympich.jpg",
			"http://iphim.vn/Shared/Posters/0a1abbd5275d42d0aa5f171e6b4d14e7Ga-chan-tinh-tot-bung-2h.jpg",
			"http://iphim.vn/Shared/Posters/1cf0f470886c4db2a7bf2851d21d98edTau-thoat-khoi-vinh-Guantanamoh.jpg",
			"http://iphim.vn/Shared/Posters/cb9eff824ffd4c8e99e42c084456c857Banh-My-Tai-ngoh.jpg",
			"http://iphim.vn/Shared/Posters/80dcffe0578c41cfb4c23420cca3df5dSieu-anh-hung-2h.jpg",
			"http://iphim.vn/Shared/Posters/b30898bb59a2418fb9643e145d9a06cfTong-tan-congh.jpg",
			"http://iphim.vn/Shared/Posters/547ef15094eb4532a2bb3e67d44f7347ao-giac-ma-quaih.jpg",
			"http://iphim.vn/Shared/Posters/13110f846ecb4a47aca94d4392428341Banh-Myh.jpg",
			"http://iphim.vn/Shared/Posters/378ef5d4e4c0496a903c5ceb39a4ba61Ba-chang-ngu-lam-2h.jpg",
			"http://iphim.vn/Shared/Posters/4e688de11a4f44e6aa065d4fe84f5905Song-not-ngay-maih.jpg",
			"http://iphim.vn/Shared/Posters/2e0655a42b37426ebf8a69cd06f3f7e7Banh-My-3-dam-cuoih.jpg",
			"http://iphim.vn/Shared/Posters/923fae44a2c843d9a2116b1920395813Com-hoc-duongh.jpg",
			"http://iphim.vn/Shared/Posters/78ad22aab2be403db5dc6b19ec7470bfGa-chan-tinh-tot-bung-3h.jpg",
			"http://iphim.vn/Shared/Posters/1a01962b2fad47cb9d636dfaa5f5fa76Co-nang-xau-tinhh.jpg",
			"http://iphim.vn/Shared/Posters/0529afd4d7244562977d7083f4a1a879Tan-cong-tram-xang-2h.jpg",
			"http://iphim.vn/Shared/Posters/70d3301a84ec464b9d0d71a0dbe4954bBat-tinh-bao-hyh.jpg",
			"http://iphim.vn/Shared/Posters/415dfd648829413f979c74204a05a296Ga-chan-tinh-tot-bungh.jpg",
			"http://iphim.vn/Shared/Posters/b5f82f1f87d741ea8e01752beb367359dua-con-cua-mat-nah.jpg", };
	private static final String KEY_CONTENT = "TestFragment:Content";

	public static FragmentCategory newInstance(String content) {
		FragmentCategory fragment = new FragmentCategory();
		fragment.mContent = content;

		return fragment;
	}

	private String mContent = "???";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if ((savedInstanceState != null)
				&& savedInstanceState.containsKey(KEY_CONTENT)) {
			mContent = savedInstanceState.getString(KEY_CONTENT);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TextView text = new TextView(getActivity());
		// text.setGravity(Gravity.CENTER);
		// text.setText(mContent);
		// text.setTextSize(20 * getResources().getDisplayMetrics().density);
		// text.setPadding(20, 20, 20, 20);
		//
		// LinearLayout layout = new LinearLayout(getActivity());
		// layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
		// LayoutParams.FILL_PARENT));
		// layout.setGravity(Gravity.CENTER);
		// layout.addView(text);
		ViewGroup view = (ViewGroup) inflater.inflate(
				R.layout.fragment_category, null);
		lsvApp = (ListView) view.findViewById(R.id.listView1);
		getData();
		adapter = new CategoryAdapter(getActivity(), listApp);
		lsvApp.setAdapter(adapter);
		implementListener();
		return view;
	}

	private void getData() {

		int i;
		Obj_App item;
		for (i = 0; i < name.length; i++) {
			item = new Obj_App();
			item.setName(name[i]);
			item.setIcon(icon[i]);
			item.setRate(rate[i]);
			item.setDeveloper(develop[i]);
			item.setTypeapp(type[i]);
			listApp.add(item);
		}
	}

	private void implementListener() {
		lsvApp.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), DetailActivity.class);
				Bundle rb = new Bundle();
				rb.putString("id", KEY_CONTENT);
//				rb.putParcelable("app", listApp.get(arg2));
				intent.putExtras(rb);
				startActivity(intent);
			}
		});
		lsvApp.setOnScrollListener(new AbsListView.OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
				int lastInScreen = firstVisibleItem + visibleItemCount;
				if ((lastInScreen == totalItemCount - 1 || lastInScreen == totalItemCount
						&& firstVisibleItem > 0)
						&& !isLoading && isLoadMore) {
					MyTaskLoadMore myTask = new MyTaskLoadMore();
					myTask.execute();
				}
			}
		});
	}

	private void loadMore() {
		int i;
		Obj_App item;
		for (i = 0; i < 5; i++) {
			item = new Obj_App();
			item.setName(name[i]);
			item.setIcon(icon[i]);
			item.setRate(rate[i]);
			item.setDeveloper(develop[i]);
			item.setTypeapp(type[i]);
			listApp.add(item);
		}
//		adapter.notifyDataSetChanged();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString(KEY_CONTENT, mContent);
	}

	public class MyTaskLoadMore extends AsyncTask<String, Void, Boolean> {
		private ProgressDialog mDialog;
		private String mResponses;
		private boolean mFinish = false;

		@Override
		protected void onPreExecute() {
			isLoading = true;
			mDialog = new ProgressDialog(getActivity());
			mDialog.setCancelable(false);
			mDialog.show();
		}

		@Override
		protected void onPostExecute(final Boolean success) {
			mDialog.cancel();
			isLoading = false;
			adapter.notifyDataSetChanged();
		}

		@Override
		protected Boolean doInBackground(final String... args) {
			loadMore();
			return true;
		}

	}
}
